#define  _CRT_SECURE_NO_WARNINGS

#include<iostream>

using namespace std;

struct TreeNode
{
    int val;
    TreeNode* left;
    TreeNode* right;
};

typedef struct TreeNode TreeNode;

int TreeNodeSize(TreeNode* root)
{
    if (root == NULL)
    {
        return 0;
    }
    return 1 + TreeNodeSize(root->left) + TreeNodeSize(root->right);
}

void PreOrder(TreeNode* root, int* pi, int* arr)
{
    if (root == NULL)
    {
        return;
    }
    arr[(*pi)] = root->val;
    (*pi)++;
    PreOrder(root->left, pi, arr);
    PreOrder(root->right, pi, arr);
}

int* preorderTraversal(struct TreeNode* root, int* returnSize)
{
    *returnSize = TreeNodeSize(root);
    int* arr = (int*)malloc(sizeof(int) * (*returnSize));
    int i = 0;
    PreOrder(root, &i, arr);
    return arr;
}